package sgi.client;

import java.io.IOException;
import java.util.Iterator;

import sgi.details_new.SubscribeDetails;
import sgi.message.MessageGetDataLayer;
import sgi.message.MessageGetDateTime;
import sgi.message.MessageGetImagePack;
import sgi.message.MessageAcceptDataLayer;
import sgi.message.MessageAcceptDateTime;
import sgi.message.MessageAcceptImagePack;

public class SGIReviewSubscriptionsController extends SGIController {

	public static SGIReviewSubscriptionsController subscriptionsReview;
	public static SGIReviewSubscriptionsUI subscriptionsReviewUI;
	/** create controller */
	public static SGIReviewSubscriptionsController createSubscriptions() throws IOException
	{
		if(SGIReviewSubscriptionsController.subscriptionsReview == null)
		{
			SGIReviewSubscriptionsController.subscriptionsReview = new SGIReviewSubscriptionsController();
			subscriptionsReviewUI.setReviewSubscriptionsController(SGIReviewSubscriptionsController.subscriptionsReview);
		}
		SGIReviewSubscriptionsController.subscriptionsReviewUI.setVisible(true);
		return subscriptionsReview;
	}
	/** sending message of retrieving rows from datalayer table */
	public void ReviewSubscriptionsDataLayer()
	{
		MessageGetDataLayer msg = new MessageGetDataLayer();
		sendReviewSubscriptionsDataLayer(msg);
	}
	/** sending message of retrieving rows from datetime table  */
	public void ReviewSubscriptionsDateTime()
	{
		MessageGetDateTime msg = new MessageGetDateTime();
		sendReviewSubscriptionsDateTime(msg);
	}
	/** sending message of retrieving rows from imagepack table */
	public void ReviewSubscriptionsImagePack()
	{
		MessageGetImagePack msg = new MessageGetImagePack();
		sendReviewSubscriptionsImagePack(msg);
	}
	
	/** sending message of accepting datalayer subscription */
	public void AcceptSubscriptionsDataLayer(String userName)
	{
		MessageAcceptDataLayer msg = new MessageAcceptDataLayer(userName);
		sendAcceptSubscriptionsDataLayer(msg);
	}
	
	private void sendAcceptSubscriptionsDataLayer(MessageAcceptDataLayer msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** sending message of accepting datetime subscription */
	public void AcceptSubscriptionsDateTime(String userName)
	{
		MessageAcceptDateTime msg = new MessageAcceptDateTime(userName);
		sendAcceptSubscriptionsDateTime(msg);
	}
	
	private void sendAcceptSubscriptionsDateTime(MessageAcceptDateTime msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** sending message of accepting imagepack subscription */
	public void AcceptSubscriptionsImagePack(String userName)
	{
		MessageAcceptImagePack msg = new MessageAcceptImagePack(userName);
		sendAcceptSubscriptionsDataLayer(msg);
	}
	
	
	private void sendAcceptSubscriptionsDataLayer(MessageAcceptImagePack msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}

	private void sendReviewSubscriptionsDataLayer(MessageGetDataLayer msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	
	private void sendReviewSubscriptionsDateTime(MessageGetDateTime msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	
	private void sendReviewSubscriptionsImagePack(MessageGetImagePack msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** terminating UI */
	public static void terminate()
	{
		SGIReviewSubscriptionsController.subscriptionsReviewUI.setVisible(false);
		SGIReviewSubscriptionsController.subscriptionsReview=null;
		SGIReviewSubscriptionsController.subscriptionsReviewUI.setVisible(false);
		SGIReviewSubscriptionsController.subscriptionsReviewUI=null;
	}
	/** constructor */
	private SGIReviewSubscriptionsController() 
	{
		SGIReviewSubscriptionsController.subscriptionsReviewUI = new SGIReviewSubscriptionsUI();
	}
	
	/** handle message of inserting datalayer rows to table */
	public void handleMessagefromDataLayer(Object msg) throws IOException
	{
		Iterator<SubscribeDetails> iterator = (((MessageGetDataLayer)msg).getDataLayer()).iterator();

		while (iterator.hasNext()) {
			SGIReviewSubscriptionsController.subscriptionsReviewUI.addRows(iterator.next());
		}
	}
	
	
	/** handle message of inserting datetime rows to table */
	public void handleMessagefromDateTime(Object msg) throws IOException
	{
		Iterator<SubscribeDetails> iterator = (((MessageGetDateTime)msg).getDateTime()).iterator();

		while (iterator.hasNext()) {
			SGIReviewSubscriptionsController.subscriptionsReviewUI.addRows(iterator.next());
		}
	}
	
	/** handle message of inserting imagepack rows to table */
	public void handleMessagefromImagePack(Object msg) throws IOException
	{
		Iterator<SubscribeDetails> iterator = (((MessageGetImagePack)msg).getImagePack()).iterator();

		while (iterator.hasNext()) {
			SGIReviewSubscriptionsController.subscriptionsReviewUI.addRows(iterator.next());
		}
	}
	
	

	@Override
	void handleMessage(Object msg)
	{
		
	}
	
	
	/** getUI */
	public static SGIReviewSubscriptionsUI getUI()
	{
		return subscriptionsReviewUI;
	}

}
